package com.huluobo.kotlindemoim1911a2.extention

import androidx.fragment.app.Fragment
import com.huluobo.kotlindemoim1911a2.R
import com.huluobo.kotlindemoim1911a2.ui.fragment.ContactFragment
import com.huluobo.kotlindemoim1911a2.ui.fragment.ConversationFragment
import com.huluobo.kotlindemoim1911a2.ui.fragment.DynamicFragment

/**
 * Created by lc on 2022/10/20.
 * 1.Fragment的工厂+单例模式
 * 2.私有化构造函数,单例模式的核心
 * 3.private constructor()在kotlin中代表私有化构造函数
 * 4.因为这个类需要在外部进行调用,所以需要对外暴露一个工厂的实例,但是这个实例不能被外部创建
 */
class FragmentFactory private constructor() {
    //对外暴露工厂实例,为了外部调用
    companion object {
        val instance = FragmentFactory()
    }

    private val contact by lazy {
        ContactFragment()
    }

    private val conversation by lazy {
        ConversationFragment()
    }

    private val dynamic by lazy {
        DynamicFragment()
    }

    fun setFragment(tabId: Int): Fragment? {
        when (tabId) {
            R.id.tab_conversation -> return conversation
            R.id.tab_contacts -> return contact
            R.id.tab_dynamic -> return dynamic
        }
        return null
    }

}